﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html.jTable;

namespace Html.jTable.Demo.Controllers
{
    public class TestController : Controller
    {
        //
        // GET: /Test/

        public ActionResult Index()
        {
            var obj = new jTableOptions();
            obj.Action.ListUrl = Url.Action("List", "Test");
            obj.General.CheckBoxColumn = true;
            obj.General.ContainerHtmlElementID = "EMPTableContainer";
            obj.General.MultiSelect = true;
            obj.General.PagingEnabled = true;
            obj.General.PageSize = 10;
            obj.General.DefaultSortingColumnName = "ENAME";
            obj.Fields.Add(new OptionField()
            {
                Caption = "EMPNO",
                Key = true,
                Name = "EMPNO",
                SortingEnabled = true
            });
            obj.Fields.Add(new OptionField()
            {
                Caption = "ENAME",
                Name = "ENAME",
                Width = "25%"
            });
            obj.Fields.Add(new OptionField()
            {
                Caption = "JOB",
                Name = "JOB",
                Width = "25%"
            });
            obj.Fields.Add(new OptionField()
            {
                Caption = "MGR",
                Name = "MGR",
                Width = "25%"
            });
            obj.Fields.Add(new OptionField()
            {
                Caption = "SAL",
                Name = "SAL",
                Width = "25%"
            });
            return View(obj);
        }

        public void List()
        {
            var db = new Html.jTable.Demo.Models.DbModelDataContext();
            var data = db.EMPs.ToList();
            Response.ContentType = @"application/json";
            var obj = new jTablePagedListResponse();
            obj.Records = data.Take(10);
            obj.ResultCode = Result.OK;
            obj.TotalRecordCount = data.Count;
            Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(obj));
        }

    }
}
